home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6799 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: dawn.mmm.com!news
  2. From: kjhopps@mmm.com (Kevin J Hopps)
  3. Newsgroups: comp.lang.c++,comp.lang.c
  4. Subject: Re: Safest way to define a string
  5. Followup-To: comp.lang.c++,comp.lang.c
  6. Date: 19 Feb 1996 20:27:23 GMT
  7. Organization: 3M - St. Paul, MN  55144-1000 US
  8. Message-ID: <4gamfb$4q7@dawn.mmm.com>
  9. References: <Pine.SOL.3.91.960217124454.26354A-100000@hamlet.uncg.edu>
  10. Reply-To: kjhopps@mmm.com
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Bin . Lee (b_lee2@hamlet.uncg.edu) wrote:
  14.  
  15. > Hi: Folks:
  16.  
  17. > This is a simplest question, I think many of you know the answer, please 
  18. > help me and send me a email.
  19.  
  20. > Question is: which is the safest way to define a string ?
  21.  
  22. > a. some one told me to use the following approach:
  23. > const int STRLEN = 100;
  24. > char strTest[STRLEN + 1];
  25.  
  26. This might be the answer you'd expect from comp.lang.c, but in comp.lang.c++
  27. my advice would be to use a string class.
  28.  
  29. > Then always use strTest[STRLEN] =0;
  30. > This is OK, but if I have to pass a 2D array to function,
  31. > it will looks like 
  32.  
  33. > int TestFun( char strTest2D[][STRLEN + 1] );
  34. > This is really awkward.
  35.  
  36. > If I just want to use array, is it safe to use Array class ? or string 
  37. > class ?
  38.  
  39. If you want an array of strings, a vector<string> class would work nicely.
  40.  
  41. > Anyway, my real question is how to prevent memory overflow for array ?
  42. > can I overload global subscript operation [] and unary operator * ?
  43.  
  44. If by "memory overflow" you mean trying to store more characters in the
  45. string than STRLEN (or some other arbitrary fixed maximum) then the answer
  46. is yes, a good string class will take care of the memory management for
  47. you.  It will also provide you an index operator.  (However this is not
  48. a "global subscript operation,", but specific to the string class.)  The
  49. class may or may not implement unary *, but in any event it will not
  50. let you "walk the string" with a pointer.
  51.  
  52. > Thanks in advance, this is important for me, please reply me in mail.
  53. > I do not want to miss it.
  54.  
  55. It is proper nettiquette to read replies in the news group unless you
  56. promise to post a summary to e-mail responses.
  57. --
  58. Kevin J. Hopps                  e-mail: kjhopps@mmm.com
  59. 3M Company                      phone:  (612) 737-4643
  60. 3M Center, Bldg. 235-2D-57      fax:    (612) 737-2700
  61. St. Paul, MN 55144-1000         Opinions are my own.  I don't speak for 3M.
  62.     But 3M speaks for me -- I did not write the following line:
  63.  
  64. Opinions expressed herein are my own and may not represent those of 3M.
  65.